<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form
          @submit.native.prevent
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="书籍名称" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入书籍名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="queryParams.hidden" placeholder="请选择">
              <el-option
                v-for="dict in content_hidden"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" @click="handleAdd">新建</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" @click="batchShow">显示</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" @click="batchHidden">隐藏</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" @click="batchDelete">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="userList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="书本名称" align="center" prop="title" />
          <el-table-column label="推荐人" align="center" prop="status">
            <template #default="scope">
              <span>系统管理员</span>
            </template>
          </el-table-column>
          <el-table-column
            label="发表时间"
            align="center"
            prop="createTime"
            width="170"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.gmtCreate) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="content_hidden" :value="scope.row.hidden" />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <a
                href="javascript:;"
                style="
                  color: #358de5;
                  text-decoration: none;
                  margin-right: 10px;
                "
                @click="handleUpdate(scope.row, 1)"
                >查看</a
              >
              <a
                href="javascript:;"
                style="
                  color: #358de5;
                  text-decoration: none;
                  margin-right: 10px;
                "
                @click="handleUpdate(scope.row, 2)"
                >编辑</a
              >
              <a
                href="javascript:;"
                style="
                  color: #358de5;
                  text-decoration: none;
                  margin-right: 10px;
                "
                @click="handleDelete(scope.row)"
                >删除</a
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form
        @submit.native.prevent
        :model="form"
        :rules="rules"
        ref="userRef"
        label-width="80px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="书名" prop="title">
              <el-input
                v-model="form.title"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="作者" prop="author">
              <el-input
                v-model="form.author"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出版社" prop="publisher">
              <el-input
                v-model="form.publisher"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出品方" prop="producer">
              <el-input
                v-model="form.producer"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="副标题" prop="subtitle">
              <el-input
                v-model="form.subtitle"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出版年月" prop="publicationDate">
              <el-date-picker
                v-model="form.publicationDate"
                type="date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="页数" prop="pageCount">
              <el-input
                v-model="form.pageCount"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="定价" prop="price">
              <el-input
              type="number"
                v-model="form.price"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="装帧" prop="binding">
              <el-input
                v-model="form.binding"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="ISBN" prop="isbn">
              <el-input
                v-model="form.isbn"
                placeholder="请输入"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="前台展示">
              <el-radio-group v-model="form.hidden">
                <el-radio
                  v-for="dict in content_hidden"
                  :key="dict.value"
                  :label="Number(dict.value)"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="封面图片" prop="cover" required>
              <ImageUpload
                v-model="form.cover"
                :materialType="1"
                :fileType="['png', 'jpg']"
              ></ImageUpload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图文内容" prop="description" required>
              <Editor v-model="form.description" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="title !== '详情'">
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
    
    <script setup name="bookManage">
import {
  querybookPage,
  addBook,
  editBook,
  bookExport,
  showBook,
  hideBook,
  deleteBook,
} from "@/api/books";
const { proxy } = getCurrentInstance();
const { content_hidden } = proxy.useDict("content_hidden");
const userList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const data = reactive({
  form: {
    id: undefined,
    title: "",
    subtitle: "",
    cover: "",
    publisher: "",
    author: "",
    description: "",
    publicationDate: "",
    price: "",
    pageCount: "",
    binding: "",
    isbn: "",
    producer: "",
    hidden: 0,
	description:''
  },
  queryParams: {
    pageNum: 1,
    pageSize: 1000,
    title: undefined,
    hidden: undefined,
  },
  rules: {
    title: [
      { required: true, message: "书名不能为空", trigger: "blur" },
      {
        min: 2,
        max: 20,
        message: "用户名称长度必须介于 2 和 20 之间",
        trigger: "blur",
      },
    ],
    author: [{ required: true, message: "作者不能为空", trigger: "blur" }],
    cover: [{ required: true, message: "书籍封面不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 选择条数  */
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 查询用户列表 */
const getList = () => {
  loading.value = true;
  querybookPage(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      loading.value = false;
      userList.value = res.rows;
      total.value = Number(res.total);
    }
  );
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value = {
    pageNum: 1,
    pageSize: 1000,
    title: undefined,
    hidden: undefined,
  };
  handleQuery();
};

/** 批量显示 */
const batchShow = async () => {
  if (ids.value.length == 0) {
    proxy.$modal.msgWarning("请选择操作内容");
    return false;
  }
  let result = await showBook(ids.value.toString());
  if (result.code == 200) {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }
};

/** 批量隐藏 */
const batchHidden = async () => {
  if (ids.value.length == 0) {
    proxy.$modal.msgWarning("请选择操作内容");
    return false;
  }
  let result = await hideBook(ids.value.toString());
  if (result.code == 200) {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }
};

/** 批量删除 */
const batchDelete = async () => {
  if (ids.value.length == 0) {
    proxy.$modal.msgWarning("请选择操作内容");
    return false;
  }
  let result = await deleteBook(ids.value.toString());
  if (result.code == 200) {
    getList();
    proxy.$modal.msgSuccess("操作成功");
  }
};

/** 导出按钮操作 */
const handleExport = async () => {
  proxy.$modal.loading("文件导出中，请稍候...");
  let result = await bookExport();
  const blob = new Blob([result]);
  let link = document.createElement("a");
  link.href = URL.createObjectURL(blob);
  link.download = "书籍列表_" + new Date().getTime() + ".xlsx";
  link.click();
  proxy.$modal.closeLoading();
};

/** 重置操作表单 */
const reset = () => {
  form.value = {
    id: undefined,
    title: "",
    subtitle: "",
    cover: "",
    publisher: "",
    author: "",
    description: "",
    publicationDate: "",
    price: "",
    pageCount: "",
    binding: "",
    isbn: "",
    producer: "",
    hidden: 0,
	description:''
  };
  proxy.resetForm("userRef");
};

const handleAdd = () => {
  title.value = "新增";
  reset();
  open.value = true;
};
/** 取消按钮 */
const cancel = () => {
  open.value = false;
  changeScore.value = false;
  reset();
};

/** 删除按钮操作 */
const handleDelete = (row) => {
  proxy.$modal
    .confirm("是否确认要删除《" + row.title + "》")
    .then(() => {
      return deleteBook(row.id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};
/** 修改按钮操作 */
const handleUpdate = (row, type) => {
  reset();
  title.value = type == 1 ? "详情" : "编辑";
  form.value = {
    ...row,
  };
  open.value = true;
};

/** 提交按钮 */
const submitForm = () => {
  proxy.$refs["userRef"].validate((valid) => {
    if (valid) {
      if (form.value.id!==undefined) {
        editBook(form.value).then((response) => {
          proxy.$modal.msgSuccess("编辑成功");
          open.value = false;
          getList();
        });
      } else {
        addBook(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};

getList();
</script>
    <style lang='scss' scoped>
</style>
    